<?

/**
  @file class_base_db.php
  @brief DB data model base class
  @author lion
  @update 2007-06-20
 */

class BaseDb {

    // db connection
    var $conn;
    
    // debug mode
    var $debug;
    
    
    function BaseDb()
    /* {{{ */
    {
        $this->conn = mysql_connect('d1.igear.co.kr', 'lionkang', 'kang1509');
        mysql_select_db('lionkang', $this->conn);

        global $debug;

        if ($debug) $this->debug =  $debug;

        
    }
    /* }}} */
    

    function query($query, $stop=0)
    /* {{{ */
    {

        if (!$this->conn &&  $this->debug) echo "DB Connect Error<br>\n";
        if ($this->debug) $start_time = microtime();

        if ( !$result = mysql_query($query, $this->conn) ) {
            if ($this->debug) {
                echo "QUERY ERROR : ".mysql_error($this->conn)."<br>\n";
                echo "QUERY : ".$query."<br>";
            }
            
            if ($stop) exit();
            return 0;
        }

        if ($show_debug) {
            $end_time = microtime();
            printf("%2.5f seconds<br><br>\n", $end_time - $start_time);
        }

        return $result;
        
    }    
    /* }}} */
    
    
    /**
     * query and fetch by row
     *@param string $query : sql query string
     *@param int $offset : row offset
     */
    function queryFetchByRow($query, $offset = -1)
    /* {{{ */
    {
        if ($row = mysql_fetch_row($this->query($query))) {
            if ($offset > -1) return $row[$offset];
            else return $row;
        }
        return 0;
    }
    /* }}} */
    

    function queryFetchByArrayAll($query, $key = 0)
    /* {{{ */
    {
        $result  = $this->query($query);
        $ret_val = array();
        while ($row = mysql_fetch_array($result)) {
            if ($key) $ret_val[$row[$key]][] = $row;
            else $ret_val[] = $row;
        }
        return $ret_val;
    }
    /* }}} */
    

    /**
     * close db connection. shortcut for closeConnection
     */
    function close()
    /* {{{ */
    {
        mysql_close($this->conn);    
    }
    /* }}} */
    

    /**
     * set debug flag. ( 0, 1, 2 )
     */
    function set_debug($debug)
    /* {{{ */
    {
        $this->debug = $debug;
    }
    /* }}} */
    
}

?>